<template>
<div>
    <el-main>
    <el-breadcrumb separator="/" style="display: inline-block;margin-left: 10px">
        <el-breadcrumb-item :to="{ path: '/' }">首页</el-breadcrumb-item>
        <el-breadcrumb-item>文件管理</el-breadcrumb-item>
    </el-breadcrumb>

    <div style="margin: 10px 0">
        <div style="margin: 10px 0">
            <el-input style="width: 200px;margin-left: 5px" placeholder="请输入名称" suffix-icon="el-icon-search" v-model="name"> </el-input>
            <el-button style="margin-left: 5px" type="primary" @click="load">搜索</el-button>
            <el-button style="margin-left: 5px" type="warning" @click="reset">重置</el-button>
        </div>

        <el-upload action="http://localhost:9090/file/upload" :show-file-list="false"  :on-success="imp" style="display:inline-block;">
            <el-button type="primary">上传文件<i class="el-icon-top"></i></el-button>
        </el-upload>
        <el-popconfirm
                confirm-button-text='确定'
                cancel-button-text='我在想想'
                icon="el-icon-info"
                icon-color="red"
                title="这是一段内容确定删除吗？"
                @confirm="alldelete"
        >
            <el-button type="danger"  slot="reference" style="margin: 0px 15px" >批量删除 <i class="el-icon-remove-outline"></i></el-button>
        </el-popconfirm>


    </div>


    <el-table :data="tableData" border stripe  @selection-change="handleSelectionChange">
        <el-table-column
                type="selection"
                width="55">
        </el-table-column>
        <el-table-column width="140" prop="id" label="ID" >
        </el-table-column>
        <el-table-column prop="name" label="文件名称" >
        </el-table-column>
        <el-table-column prop="type" label="文件类型">
        </el-table-column>
        <el-table-column prop="size" label="文件大小(kb)">
        </el-table-column>
        <el-table-column  label="下载">
            <template slot-scope="scope">
                <el-button type="primary" @click="download(scope.row.url)">下载</el-button>
            </template>
        </el-table-column>
        <el-table-column prop="enable" label="是否启用">
            <template slot-scope="scope">
                <el-switch v-model="scope.row.enable" active-color="#13ce66" inactive-color="#ccc" @change="changeEnable(scope.row)"></el-switch>
            </template>
        </el-table-column>
        <el-table-column label="操作"  width="200" align="center">
            <template slot-scope="scope">

                <el-popconfirm
                        confirm-button-text='确定'
                        cancel-button-text='我在想想'
                        icon="el-icon-info"
                        icon-color="red"
                        title="这是一段内容确定删除吗？"
                        @confirm="remove(scope.row.id)"
                >
                    <el-button type="danger" slot="reference"  style="margin-left: 15px">删除 <i class="el-icon-remove-outline"></i></el-button>
                </el-popconfirm>

            </template>
        </el-table-column>
    </el-table>
    <div style="padding: 10px 0">
        <el-pagination
                @size-change="handleSizeChange"
                @current-change="handleCurrentChange"
                :current-page="pageNum"
                :page-sizes="[2, 5, 10]"
                :page-size="pageSize"
                layout="total, sizes, prev, pager, next, jumper"
                :total="total">
        </el-pagination>
    </div>
    </el-main>
</div>
</template>

<script>
    import request from "@/utils/request";

    export default {
        name: "Files",
        data(){
            return{
                tableData:[],
                multipleSelection:[],
                pageSize:10,
                pageNum:1,
                total:0,
                name:""
            }

        },
        methods:{
            handleSizeChange(pageSize){
                this.pageSize=pageSize
                this.load()
            },
            handleCurrentChange(pageNum){
                this.pageNum=pageNum
                this.load()
            },
            load(){
                request.get("http://localhost:9090/file/page?pageNum="+this.pageNum+"&pageSize="+this.pageSize+"&name="+this.name).then(res=>{
                    this.tableData=res.records
                    this.total=res.total
                })

            },
            reset(){
                this.name=""
                this.load()
            },
            newadd(){
                this.title="新增"
                this.dialogFormVisible=true
                this.form={}
            },
            Add(){
                request.post("http://localhost:9090/user",this.form).then(res=>{
                    if(res){
                        this.$message.success("成功")
                        this.load()
                    }else
                        this.$message.error("失败")
                })
                //this.form=""
                this.dialogFormVisible=false
            },
            remove(e){
                request.delete("http://localhost:9090/file/"+e).then(res=>{
                    if(res===1){
                        this.$message.success("成功")
                        this.load()
                    }else
                        this.$message.error("失败")
                })
            },
            handleSelectionChange(val){
                this.multipleSelection = val;
            },
            alldelete(){
                let ids=this.multipleSelection.map(v=>v.id)
                console.log(ids)
                this.request.post("http://localhost:9090/file/deleteAll",ids).then(res=>{
                    if(res){
                        this.$message.success("批量删除成功")
                        this.load()
                    }else
                        this.$message.error("失败")
                })
            },
            update(e){
                this.title='修改'
                this.form=e
                this.dialogFormVisible=true
            },
            imp(){
            this.load()
            },
            download(e){
                window.open(e)
            },
            changeEnable(e){
                console.log(this.tableData.id)
                this.request.post("http://localhost:9090/file/update",e).then(res=>{
                    if(res===1){
                        this.$message.success("操作成功")

                    }
                })
            }

        },

        created() {
            this.load()
        }
    }
</script>

<style scoped>

</style>